package com.zytlifeservice.controller;

import com.zytlifeservice.domain.Result;
import com.zytlifeservice.domain.User;
import com.zytlifeservice.domain.find.Commodity;
import com.zytlifeservice.domain.pay.MoblieAndFlowCost;
import com.zytlifeservice.domain.pay.ProvinceInfo;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.repository.UserRepository;
import com.zytlifeservice.repository.VerifyRepository;
import com.zytlifeservice.repository.find.CommodityRepository;
import com.zytlifeservice.repository.pay.*;
import com.zytlifeservice.service.FileUploadService;
import com.zytlifeservice.service.find.CommodityService;
import com.zytlifeservice.service.pay.AirplaneService;
import com.zytlifeservice.service.pay.AlipayService;
import com.zytlifeservice.service.pay.PublicService;
import com.zytlifeservice.service.pay.WxPayService;
import com.zytlifeservice.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by MJS on 2017/5/3.
 */

@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    VerifyRepository verifyRepository;

    @Autowired
    MoblieAndFlowCostRepository moblieAndFlowCostRepository;

    @Autowired
    MobileRechargeOrderRepository mobileRechargeOrderRepository;

    @Autowired
    AlipayOrderRepository alipayOrderRepository;

    @Autowired
    ProvinceInfoRepository provinceInfoRepository;

    @Autowired
    CommodityRepository commodityRepository;

    @Autowired
    FileUploadService fileUploadService;

    @Autowired
    WxPayService wxPayService;

    @Autowired
    WxPayOrderRepository wxPayOrderRepository;

    @Autowired
    CommodityService commodityService;

    @Autowired
    PublicService publicService;

    @Autowired
    AirplaneService airplaneService;
    @Autowired
    AlipayService alipayService;

    @GetMapping(value = "/wel")
    public String testHelloWorld() {
        return "test...";
    }

    /*
    测试时删除用户接口。
     */
    @Transactional
    @PostMapping(value = "/delete")
    public Result eleteUser(@RequestParam("mobile") String mobile) {
        return ResultUtil.success("删除用户个数=" + userRepository.deleteByMobile(mobile), "测试数据");
    }

    @Transactional
    @PostMapping(value = "/delete/verify")
    public Result deleteVerify(@RequestParam("mobile") String mobile) {
        return ResultUtil.success("删除验证码个数=" + verifyRepository.removeAllByMobile(mobile), "删除验证码");
    }

    @PostMapping(value = "/query/verify")
    public Result queryAllVerify(@RequestParam("mobile") String mobile) {
        return ResultUtil.success(verifyRepository.findByMobileOrderBySendTimeDesc(mobile), "查询验证码");
    }

    @PostMapping(value = "/addMobileAndFlow")
    public Result addMobileAndFlow(String type, String parValue, double price) {
        MoblieAndFlowCost bean = new MoblieAndFlowCost();
        bean.setParValue(parValue);
        bean.setPrice(price);
        bean.setType(type);
        moblieAndFlowCostRepository.save(bean);
        return ResultUtil.success();
    }

    @PostMapping(value = "/deleteMobileAndFlow")
    public Result deleteMobileAndFlow(Long id) {
        moblieAndFlowCostRepository.delete(id);
        return ResultUtil.success();
    }


    @GetMapping(value = "/queryAllAlipayOrder")
    public Result queryAlipayOrder() {
        return ResultUtil.success(alipayOrderRepository.findAll(), "success");
    }


    @PostMapping(value = "/addProvinceInfo")
    public Result addProvinceInfo(String provinceName) {
        ProvinceInfo info = new ProvinceInfo();
        info.setName(provinceName);
        provinceInfoRepository.save(info);
        return ResultUtil.success();
    }

    @PostMapping(value = "/deleteProvinceInfo")
    public Result deleteProvinceInfo(Long id) {
        provinceInfoRepository.delete(id);
        return ResultUtil.success();
    }

    //添加商品，带名称
    @PostMapping(value = "/addCommodity")
    public Result addCommodityByFile(int pneedIntegral, String pexplain, String pname,
                                     int pcount, @RequestParam("image") MultipartFile[] files) {

        if (files != null && files.length >= 1) {
            String icon = fileUploadService.storeBaseIcon(files[0]);
            Commodity commodity = new Commodity();
            commodity.setPname(pname);
            commodity.setPcount(pcount);
            commodity.setPexplain(pexplain);
            commodity.setPneedIntegral(pneedIntegral);
            commodity.setPimageUrl(icon);
            commodityRepository.save(commodity);
            return ResultUtil.success();
        }
        return ResultUtil.error(303, "操作失败");
    }

    //删除商品
    @PostMapping(value = "/deleteCommodity")
    public Result deleteCommodity(long id) {
        Commodity one = commodityRepository.findOne(id);
        String path = one.getPimageUrl();
        fileUploadService.delete(path);
        commodityRepository.delete(id);
        return ResultUtil.success();
    }

    //测试更新用户积分
    @PostMapping(value = "/updateUserIntegral")
    public Result updateUserIntegral(long userId, int integral) {
        User one = userRepository.findOne(userId);
        if (one == null) {
            throw new UserException("用户不存在", 202);
        }
        one.setUserIntegral(integral);
        return ResultUtil.success(userRepository.save(one), "修改成功");
    }


    @GetMapping(value = "/updateToken")
    public Result testUpdateToken() {
        return ResultUtil.success(publicService.updateToken(), "suc");
    }

    @PostMapping(value = "/wxRefund")
    public Result testRefund(String outTradeNo) {
        String refund = wxPayService.refund(wxPayOrderRepository.findByOutTradeNo(outTradeNo), "");
        return ResultUtil.success();
    }


    @GetMapping(value = "/testFindAllPhoneOrder")
    public Result testQueryAllPhone() {
        return ResultUtil.success(mobileRechargeOrderRepository.findAll(), "suc");
    }


    @PostMapping(value = "/queryAllWxOrder")
    public Result testQueryAllWxOrder() {
        return ResultUtil.success(wxPayOrderRepository.findAll(), "查询成功");
    }


    @PostMapping(value = "/testPayAir")
    public Result testPayAir(String outTradeNo) {
        String pay = airplaneService.pay(outTradeNo);
        return ResultUtil.success(pay, "查询成功");
    }


    @GetMapping(value = "/closeQmClient")
    public Result closeQMClient() {
        publicService.closeQmCsClient();
        return ResultUtil.success();
    }

    @GetMapping(value = "/openQmClient")
    public Result openQmClient() {
        publicService.initBMMsg();
        return ResultUtil.success();
    }


    @PostMapping(value = "/updateUserMchId")
    public Result updateUserMchId(long userId, String shopName, String ali_mch_id, String wx_mch_id) {
        User one = userRepository.findOne(userId);
        if (one == null) {
            throw new UserException("用户不存在", 303);
        }
        one.setShopName(shopName);
        one.setAli_mch_id(ali_mch_id);
        one.setWx_mch_id(wx_mch_id);
        userRepository.save(one);
        return ResultUtil.success();
    }


    @PostMapping(value = "/queryAliOrder")
    public Object testQueryAliOrder(String outTradeNo) {
        return alipayService.query(outTradeNo);
    }

//    @PostMapping(value = "/alipayRefund")
//    public Object testFund(String outTradeNo,String money) {
//
//    }
}